package com.uit.smarttool.ultility;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.uit.smarttool.activity.LoginActivity;
import com.uit.smarttool.activity.R;
import com.uit.smarttool.activity.Tab1Activity;

public class CustomAdapter extends ArrayAdapter<String> {

	private final Context context;
	private final List<String> values;
	private final List<String> urlImage;
	private final List<String> dongia;
	static String result = "";
	ImageView imageView;

	public CustomAdapter(Context context, List<String> values,
			List<String> urlImage, List<String> dongia) {
		super(context, R.layout.categories, values);
		this.context = context;
		this.values = values;
		this.urlImage = urlImage;
		this.dongia = dongia;
	}

	static Bitmap bmp;

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.categories, parent, false);
		TextView textView = (TextView) rowView.findViewById(R.id.label);
		TextView ds = (TextView) rowView.findViewById(R.id.labeldg);
		imageView = (ImageView) rowView.findViewById(R.id.logo);
		textView.setText(values.get(position));
		ds.setText(dongia.get(position));
		
		new LoadImage(imageView).execute(urlImage.get(position).toString());
	
		return rowView;
	}
	
	// create new cart after checkout
	/*private class getImage extends Thread {
		public void run() {
			try {
				if (bmp == null && imageView == null) {
					CustomAdapter.result = "START";
				} else {
					CustomAdapter.result = "Finish";
				}

			} catch (Exception ex) {
				CustomAdapter.result = ex.toString();
			}
		}
	}*/
	
	public class LoadImage extends AsyncTask<String, Void, Bitmap> {
	    ImageView imgView;
	    public LoadImage(ImageView img) {
	        this.imgView = img;
	    }
	    @Override
	    protected Bitmap doInBackground(String... urls) {
	        String urlDisplay = urls[0];
	        Bitmap img = null;
	        try {
	            InputStream in = new java.net.URL(urlDisplay).openStream();
	            img = BitmapFactory.decodeStream(in);
	        } catch (MalformedURLException ex) {
	           
	        	// TODO Auto-generated catch block
	            ex.printStackTrace();
	        } catch (IOException ex) {
	            Log.e("error", ex.getMessage());
	            ex.printStackTrace();
	        }
	        return img;
	    }
	    @Override
	    protected void onPostExecute(Bitmap result) {
	        this.imgView.setImageBitmap(result);
	    }
	}
}
